package vincent.com.layouttest;

//import android.app.ActionBar;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.graphics.Color;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import vincent.com.layouttest.anim.PropertyAnimSampleActivity;
import vincent.com.layouttest.service.AIDLServiceActivities;
import vincent.com.layouttest.service.MessengerServiceActivities;
import vincent.com.layouttest.service.MyServiceActivities;
import vincent.com.layouttest.surface.SurfaceViewActivity;
import vincent.com.layouttest.util.LogUtil;
import vincent.com.layouttest.util.ReflectHelper;
import vincent.com.layouttest.util.Utils;
import vincent.com.layouttest.view.SelectTFCardDialog;

//import android.support.v4.app.ActionBarDrawerToggle;

/**
 * Created by chenzhengwen on 2015/3/16.
 */
public class DrawerTopOnActionBar extends ActionBarActivity implements View.OnClickListener, SelectTFCardDialog.OnBtnClickListener{
    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle toggle;
    private ListView navigateList;
    private LinearLayout container;

    private Button dynamicLoadDexBtn, circleBtn, colorStatus, roundShowImg, showWebView,
            transparentActionbar, circleSwitch, brokenLineBtn, coordinatorLayout,
            snifferLanBtn, toolbarTestBtn, customViewGroupBtn, hiddenDrawerLayoutBtn, gestureDetectBtn, limitationUCBtn, drawDrawableBtn,
            binderServiceBtn, messengeServiceBtn, aidlServiceBtn, surfaceDrawBtn, openglSampleBtn, propertyAnimBtn, uvSampleBtn, mixColorBtn;

    private UsbManager mUsbManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_drawer_top_on_actionbar);

        mUsbManager = (UsbManager) this.getSystemService(Context.USB_SERVICE);

        IntentFilter ifilter = new IntentFilter();
        ifilter.addAction(Intent.ACTION_POWER_CONNECTED);
        ifilter.addAction(Intent.ACTION_POWER_DISCONNECTED);

        this.registerReceiver(mReceiver, ifilter);

        initContent();

        circleBtn = (Button)findViewById(R.id.circle_btn);
        circleBtn.setOnClickListener(this);

       /* colorStatus = (Button)findViewById(R.id.color_status);
        colorStatus.setOnClickListener(this);*/

        roundShowImg = (Button)findViewById(R.id.round_show_img);
        roundShowImg.setOnClickListener(this);

        /*showWebView = (Button)findViewById(R.id.show_webview);
        showWebView.setOnClickListener(this);*/

       /* transparentActionbar = (Button)findViewById(R.id.transparent_actionbar);
        transparentActionbar.setOnClickListener(this);
*/
        circleSwitch = (Button)findViewById(R.id.circle_switch);
        circleSwitch.setOnClickListener(this);

       /* brokenLineBtn = (Button)findViewById(R.id.broken_line_chart_btn);
        brokenLineBtn.setOnClickListener(this);*/

        coordinatorLayout = (Button)findViewById(R.id.coordinator_layout);
        coordinatorLayout.setOnClickListener(this);

        /*snifferLanBtn = (Button)findViewById(R.id.sniffer_lan);
        snifferLanBtn.setOnClickListener(this);

        toolbarTestBtn = (Button)findViewById(R.id.toolbar_test);
        toolbarTestBtn.setOnClickListener(this);*/

        customViewGroupBtn = (Button)findViewById(R.id.custom_viewgroup);
        customViewGroupBtn.setOnClickListener(this);

        hiddenDrawerLayoutBtn = (Button)findViewById(R.id.hiddendrawer_layout);
        hiddenDrawerLayoutBtn.setOnClickListener(this);

        gestureDetectBtn = (Button)findViewById(R.id.gesture_detect);
        gestureDetectBtn.setOnClickListener(this);

        limitationUCBtn = (Button)findViewById(R.id.drawer_demo);
        limitationUCBtn.setOnClickListener(this);

        drawDrawableBtn = (Button)findViewById(R.id.draw_drawable_btn);
        drawDrawableBtn.setOnClickListener(this);

        binderServiceBtn = (Button)findViewById(R.id.binder_service);
        binderServiceBtn.setOnClickListener(this);

        messengeServiceBtn = (Button)findViewById(R.id.messenger_service);
        messengeServiceBtn.setOnClickListener(this);

        aidlServiceBtn = (Button)findViewById(R.id.aidl_service);
        aidlServiceBtn.setOnClickListener(this);

        surfaceDrawBtn = (Button)findViewById(R.id.surface_draw);
        surfaceDrawBtn.setOnClickListener(this);

        openglSampleBtn = (Button)findViewById(R.id.opengl_sample);
        openglSampleBtn.setOnClickListener(this);

        propertyAnimBtn = (Button)findViewById(R.id.property_anim);
        propertyAnimBtn.setOnClickListener(this);

        uvSampleBtn = (Button)findViewById(R.id.uv_sample);
        uvSampleBtn.setOnClickListener(this);

        dynamicLoadDexBtn = (Button)findViewById(R.id.dynamic_load_dex);
        dynamicLoadDexBtn.setOnClickListener(this);

//        mixColorBtn = (Button)findViewById(R.id.mix_color);
//        mixColorBtn.setOnClickListener(this);

        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);
        initActionBar();

        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        drawerLayout.setScrimColor(Color.argb(100, 0, 0, 0));
        toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open_drawer, R.string.close_drawer){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                invalidateOptionsMenu();
                syncState();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                invalidateOptionsMenu();
                syncState();
            }
        };

        drawerLayout.setDrawerListener(toggle);

        navigateList = (ListView)findViewById(R.id.navigate_list);

        String[] options = new String[]{"option1", "option2", "option3", "option4", "option5"};
        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, options);
        navigateList.setAdapter(adapter);

        ListAdapter adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, options);
        ListView navigateList2 = (ListView)findViewById(R.id.navigate_list_2);
        navigateList2.setAdapter(adapter2);

        toggle.syncState();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mReceiver != null){
            this.unregisterReceiver(mReceiver);
        }
    }

    public void initContent(){
        drawerLayout = (DrawerLayout) LayoutInflater.from(this).inflate(R.layout.drawer, null);
        LinearLayout content = (LinearLayout)drawerLayout.findViewById(R.id.drawer_content);
        FrameLayout realContent = (FrameLayout)drawerLayout.findViewById(R.id.real_content);

        if(Build.VERSION.SDK_INT >= 19){
           /* getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            setContentView(R.layout.activity_drawer_top_on_actionbar);

            ViewGroup decor = (ViewGroup)getWindow().getDecorView();
            View child = decor.getChildAt(0);
            decor.removeView(child);

//            drawerLayout.setPadding(0, getStatusBarHeight(), 0, 0);

            realContent.setPadding(0, getStatusBarHeight(), 0, 0);
            realContent.addView(child);

            decor.addView(drawerLayout);*/

            View v = LayoutInflater.from(this).inflate(R.layout.activity_drawer_top_on_actionbar, null);

            drawerLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

//            drawerLayout.setPadding(0, getStatusBarHeight(), 0, 0);
            content.setPadding(0, Utils.getStatusBarHeight(this), 0, 0);
            realContent.addView(v);

            setContentView(drawerLayout);
        }else {
            setContentView(drawerLayout);

            View v = LayoutInflater.from(this).inflate(R.layout.activity_drawer_top_on_actionbar, null);
            realContent.addView(v);
        }
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(Intent.ACTION_POWER_CONNECTED.equals(action)){
                Object result = ReflectHelper.callDeclaredMethod(mUsbManager, "getDefaultFunction", null, null);
                //当前硬件真实的USB模式
                String realUSBMode = (String)result;
                Toast.makeText(DrawerTopOnActionBar.this, "连接上电源充电--->usb mode: " + realUSBMode , Toast.LENGTH_SHORT).show();
            }else if(Intent.ACTION_POWER_DISCONNECTED.equals(action)){
                Toast.makeText(DrawerTopOnActionBar.this, "断开电源充电", Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        toggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        toggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(toggle.onOptionsItemSelected(item)){
            return true;
        }

        return super.onOptionsItemSelected(item);
    }



    public void initActionBar(){
//        ActionBar actionBar = getActionBar();
//        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.circle_btn:
                Intent intent = new Intent(this, CircleImageActivity.class);
                startActivity(intent);
                break;
           /* case R.id.color_status:
                Intent intent2 = new Intent(this, ColorStatusActivity.class);
                startActivity(intent2);
                break;*/
            case R.id.round_show_img:
                Intent intent3 = new Intent(this, RoundShowCircleImageActivity.class);
                startActivity(intent3);
                break;
            /*case R.id.show_webview:
                Intent intent4 = new Intent(this, WebViewActivity.class);
                startActivity(intent4);
                break;
            case R.id.transparent_actionbar:
                Intent intent5 = new Intent(this, TransparentActivity.class);
                startActivity(intent5);
                break;*/
            case R.id.circle_switch:
                Intent intent6 = new Intent(this, CircleSwitchActivity.class);
                startActivity(intent6);
                break;
            /*case R.id.broken_line_chart_btn:
                Intent intent7 = new Intent(this, BrokenLineChartActivity.class);
                startActivity(intent7);
                break;*/
            case R.id.coordinator_layout:
                Intent intent8 = new Intent(this, CoordinatorLayoutActivity.class);
                startActivity(intent8);
                break;
           /* case R.id.sniffer_lan:
                Intent intent9 = new Intent(this, LANSnifferActivity.class);
                startActivity(intent9);
                break;*/
            /*case R.id.toolbar_test:
                break;*/
            case R.id.custom_viewgroup:
                Intent intent10 = new Intent(this, CustomViewGroupActivity.class);
                startActivity(intent10);
                break;
            case R.id.hiddendrawer_layout:
                Intent intent11 = new Intent(this, HiddenDrawerLayoutActivity.class);
                startActivity(intent11);
                break;
            case R.id.gesture_detect:
                Intent intent12 = new Intent(this, GestureDetectActivity.class);
                startActivity(intent12);
                break;
            case R.id.drawer_demo:
                Intent intent13 = new Intent(this, DrawerDemoActivity.class);
                startActivity(intent13);
                break;
            case R.id.draw_drawable_btn:
                Intent intent14 = new Intent(this, CanvasDrawableActivity.class);
                startActivity(intent14);
                break;
            case R.id.binder_service:
                Intent intent15 = new Intent(this, MyServiceActivities.class);
                startActivity(intent15);
                break;
            case R.id.messenger_service:
                Intent intent16 = new Intent(this, MessengerServiceActivities.class);
                startActivity(intent16);
                break;
            case R.id.aidl_service:
                Intent intent17 = new Intent(this, AIDLServiceActivities.class);
                startActivity(intent17);
                break;
            case R.id.surface_draw:
                Intent intent18 = new Intent(this, SurfaceViewActivity.class);
                startActivity(intent18);
                break;
            case R.id.opengl_sample:
                Intent intent19 = new Intent(this, OpenGLSampleActivity.class);
                startActivity(intent19);
                break;
            case R.id.property_anim:
                Intent intent20 = new Intent(this, PropertyAnimSampleActivity.class);
                startActivity(intent20);
                break;
            case R.id.uv_sample:
                Intent intent21 = new Intent(this, UVSampleActivity.class);
                startActivity(intent21);
                break;
           /* case R.id.mix_color:
                Intent intent22 = new Intent(this, MixColorActivity.class);
                startActivity(intent22);
                break;*/
            case R.id.dynamic_load_dex:
                Intent intent22 = new Intent(this, DynamicLoadDexActivity.class);
                startActivity(intent22);
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    public void dynamicProxy(View v){
        startActivity(new Intent(this, DynamicProxyActivity.class));
    }

    public void dialog(View v){
        if(mDialog == null){
            mDialog = new SelectTFCardDialog(this, this);
        }
        mDialog.show();
    }

    private SelectTFCardDialog mDialog;

    @Override
    public void onClick(int index) {

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        String from = intent.getStringExtra("from");
        LogUtil.i("czw", "主页from--->" + from);
    }

    public void getDevProp(View v){
        startActivity(new Intent(this, GetDevPropActivity.class));
    }

    public void bluetooth(View v){
        startActivity(new Intent(this, BluetoothActivity.class));
    }

    public void showSystemIcons(View v){
        startActivity(new Intent(this, SystemIconsActivity.class));
    }

    public void wifi(View v){
        startActivity(new Intent(this, WifiActivity.class));
    }

    public void activityStartMode(View view){
        startActivity(new Intent(this, ActStartModeActivity.class));
    }

    public void scrollDemo(View view){
        startActivity(new Intent(this, ScrollerDemoActivity.class));
    }
}
